﻿using System.Net.Sockets;
using System.Net;
using System;
using System.Threading;

namespace Npu_Platform
{
    class Server : Network
    {
        private Socket _listener;
        public Server(IPAddress ServerIpav6, int ListenPort)
            : base(ServerIpav6, ListenPort)
        {

        }
        public Boolean ServerInit(out String s, out Exception e)
        {
            s = "";
            e = new Exception();
            if (Ipv6Support() == false)
            {
                return false;
            }
            else
            {
                try
                {
                    this._Ipep = new IPEndPoint(this._Ipav6, this._port);
                    _listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                    _listener.Bind(_Ipep);
                    _listener.Listen(0);
                    Socket socket = _listener.Accept();
                    _listener.Close();

                    byte[] b = new byte[11];
                    int len;
                    while ((len = socket.Receive(b)) != 0)
                    {
                        s = "RX: " + System.Text.ASCIIEncoding.ASCII.GetString(b, 0, len);
                        b = new byte[11];
                    }
                    socket.Close();
                    return true;
                }
                catch (Exception ex)
                {
                    e = ex;
                    return false;
                }

            }
        }
        public Boolean ServerInit(out String s)
        {
            s = "";
            if (Ipv6Support() == false)
            {
                return false;
            }
            else
            {
               
                    this._Ipep = new IPEndPoint(this._Ipav6, this._port);
                    _listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                    _listener.Bind(_Ipep);
                    _listener.Listen(5);
                    Socket socket = _listener.Accept();
                    _listener.Close();

                    byte[] b = new byte[11];
                    int len;
                    while ((len = socket.Receive(b)) != 0)
                    {
                        s = "RX: " + System.Text.ASCIIEncoding.ASCII.GetString(b, 0, len);
                        b = new byte[11];
                    }
                    socket.Close();
                    return true;
                }
               

            }
        public void ServerInit()
        {
            
            if (Ipv6Support() == false)
            {
                return;
            }
            else
            {
                try
                {
                    this._Ipep = new IPEndPoint(this._Ipav6, this._port);
                    _listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                    _listener.Bind(_Ipep);
                    _listener.Listen(5);
                    while (true)
                    {
                        Socket socket = _listener.Accept();
                        ThreadPool.QueueUserWorkItem(new WaitCallback(Receive),socket); 
                    }
                    //_listener.Close();

                    
                    
                    
                }
                catch (System.Exception ex)
                {
                    if (SendError != null)
                    {
                        SendError(ex);
                    }
                    
                }
                
            }


        }
        public void Receive(object so)
        {
            Socket socket = (Socket)so;
            String s;
            byte[] b = new byte[11];
            int len;
            while ((len = socket.Receive(b)) != 0)
            {
                s = "RX: " + System.Text.ASCIIEncoding.ASCII.GetString(b, 0, len);
                if (SendMessage != null)
                {
                    SendMessage(s);
                }
                b = new byte[11];
                
            }
            socket.Close();
        }
        public delegate void SendMessageEventHandler(String s);
        public event SendMessageEventHandler SendMessage;
        public delegate void SendErrorEventHandler(Exception ex);
        public event SendErrorEventHandler SendError;


    }
}